﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class GioHang : System.Web.UI.Page
{
    Cart gh;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            view();
    }
    public void view()
    {
        if (Session["Giohang"] != null)
        {
            DataTable dt = (DataTable)Session["Giohang"];

            if (dt.Rows.Count == 0)
            {
                Panel1.Visible = true;
                Panel2.Visible = false;
            }
            else
            {
                Panel1.Visible = false;
                Panel2.Visible = true;
                tinhtien(dt);
                gridGiohang.DataSource = dt;
                gridGiohang.DataBind();
            }
        }
        else
        {
            Panel1.Visible = true;
            Panel2.Visible = false;
        }
    }
    private void tinhtien(DataTable tb)
    {
        double tien = 0;
        foreach (DataRow dr in tb.Rows)
        {
            tien += double.Parse(dr[4].ToString());
        }
        lblTongtien.Text = tien.ToString();
    }
    protected void ibtnThemhang_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("Default.aspx");
    }


    protected void ibtnCapnhat_Click(object sender, EventArgs e)
    {
        gh = new Cart();
        DataTable dt = (DataTable)Session["Giohang"];
        foreach (GridViewRow gr in gridGiohang.Rows)
        {
            TextBox txtsl = (TextBox)gr.FindControl("txtSoluong");
            if (int.Parse(txtsl.Text) > 0)
                dt = gh.capNhatlai(dt, double.Parse(txtsl.Text), gr.Cells[1].Text);
            else if (int.Parse(txtsl.Text) == 0)
            {
                dt = gh.xoaDong(dt, gr.Cells[1].Text);
                if (dt.Rows.Count == 0)
                {
                    Panel1.Visible = true;
                    Panel2.Visible = false;
                }
            }
        }
        gridGiohang.DataSource = dt;
        gridGiohang.DataBind();
        tinhtien(dt);
        Session["Giohang"] = dt;
    }

    protected void gridGiohang_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        gh = new Cart();
        DataTable tb = (DataTable)Session["Giohang"];
        tb = gh.xoaDong(tb, gridGiohang.Rows[e.RowIndex].Cells[1].Text);
        gridGiohang.DataSource = tb;
        gridGiohang.DataBind();
        Session["Giohang"] = tb;
        if (Session["Giohang"] != null)
        {
            DataTable dt = (DataTable)Session["Giohang"];
            if (dt.Rows.Count == 0)
            {
                Panel1.Visible = true;
                Panel2.Visible = false;
            }
        }
    }


    protected void gridGiohang_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gridGiohang.PageIndex = e.NewPageIndex;
        view();
    }

    protected void gridGiohang_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        gh = new Cart();
        DataTable dt = (DataTable)Session["Giohang"];
        foreach (GridViewRow gr in gridGiohang.Rows)
        {
            TextBox txtsl = (TextBox)gr.FindControl("txtSoluong");
            if (int.Parse(txtsl.Text) > 0)
                dt = gh.capNhatlai(dt, double.Parse(txtsl.Text), gr.Cells[1].Text);
            else
            {
                dt = gh.xoaDong(dt, gr.Cells[1].Text);
                if (dt.Rows.Count == 0)
                {
                    Panel1.Visible = true;
                    Panel2.Visible = false;
                }
            }
        }
        gridGiohang.DataSource = dt;
        gridGiohang.DataBind();
        tinhtien(dt);
        Session["Giohang"] = dt;
    }
    protected void ibtnThanhtoan_Click(object sender, ImageClickEventArgs e)
    {
        DataTable tb = new DataTable();
        tb.Columns.Add("TenSP");
        tb.Columns.Add("SoLuong");
        tb.Columns.Add("Gia");
        tb.Columns.Add("TongGia");
        tb.Columns.Add("Masp");

        foreach (GridViewRow row in gridGiohang.Rows)
        {
            string s = row.Cells[1].Text;
            Label lblMasp = (Label)row.FindControl("lblMaSP");
            TextBox soluong = (TextBox)row.FindControl("txtSoluong");
            Label lbldongia = (Label)row.FindControl("lblDongia");
            Label lbltonggia = (Label)row.FindControl("lblTonggia");
            string sl = soluong.Text;
            string dongia = lbldongia.Text;
            string tonggia = lbltonggia.Text;
            string masp = lblMasp.Text;
            object[] dl = new object[5];
            dl[0] = s;
            dl[1] = sl;
            dl[2] = dongia;
            dl[3] = tonggia;
            dl[4] = masp;
            tb.Rows.Add(dl);
        }

        Session["Hangmua"] = tb;
    
        Session["Tong"] = lblTongtien.Text;
        Response.Redirect("Kiemtragiohang.aspx");
    }
}